#include <copydir.h>
//将文件名按ASCII码排序
int cmp(const void *a, const void *b) {
	struct file_quality *c = (struct file_quality *)a;
	struct file_quality *d = (struct file_quality *)b;
	return strcmp(c->name,d->name);
}

int q_directory(char *path, struct file_quality ch[]) {
	DIR *dir = opendir(path);
	struct dirent *entry;
	struct stat buf;
	int len,i = 0;
	i = 0;
	if(dir != NULL) {
		while((entry = readdir(dir)) != NULL) {
			if(!strcmp(entry->d_name,".")||!strcmp(entry->d_name,".."))
				continue;
			strcpy(ch[i].name,entry->d_name);
			if(entry->d_type & DT_DIR)		ch[i].type = 1;
			else if(entry->d_type & DT_REG)	ch[i].type = 2;
			else if(entry->d_type & DT_FIFO)	ch[i].type = 3;
			else					ch[i].type = 4;
			char pathname[D_SIZE];
			sprintf(pathname, "%s/%s", path, entry->d_name);
			stat(pathname, &buf);
			struct tm *time = gmtime(&buf.st_mtime);
			strftime(ch[i].time,30,"%Y-%m-%d-%H-%M-%S", time);
			i++;
		}
		len = i;
		qsort(ch, len, sizeof(ch[0]), cmp);
	}
	FILE *f = fopen("./pro_copy.log","a+");
	time_t rawtime;
	struct tm *timeinfo;
	timeinfo = localtime(&rawtime);
	fprintf(f,"\007The current date/time is:%s\n",asctime(timeinfo));
	fprintf(f,"qsort directory %s\n",path);
	fclose(f);
	return len;
}
